//
//  SMNewsCell.m
//  SteelRecover
//
//  Created by 郑渊文 on 2025/9/16.
//

#import "SMNewsCell.h"

#import "SMNewsCell.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface SMNewsCell ()
@property(nonatomic, strong) UIView *card;
@property(nonatomic, strong) UIImageView *thumb;
@property(nonatomic, strong) UILabel *titleL;
@property(nonatomic, strong) UILabel *introL;
@property(nonatomic, strong) UILabel *timeL;
@end

@implementation SMNewsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = UIColor.clearColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        _card = [UIView new];
        _card.backgroundColor = UIColor.secondarySystemBackgroundColor;
        _card.layer.cornerRadius = 12;
        _card.layer.masksToBounds = YES;
        [self.contentView addSubview:_card];

        _thumb = [UIImageView new];
        _thumb.contentMode = UIViewContentModeScaleAspectFill;
        _thumb.clipsToBounds = YES;

        _titleL = [UILabel new];
        _titleL.font = [UIFont boldSystemFontOfSize:16];
        _titleL.numberOfLines = 2;

        _introL = [UILabel new];
        _introL.font = [UIFont systemFontOfSize:13];
        _introL.textColor = UIColor.secondaryLabelColor;
        _introL.numberOfLines = 2;

        _timeL = [UILabel new];
        _timeL.font = [UIFont systemFontOfSize:12];
        _timeL.textColor = UIColor.tertiaryLabelColor;

        [_card addSubview:_thumb];
        [_card addSubview:_titleL];
        [_card addSubview:_introL];
        [_card addSubview:_timeL];

        [_card mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(8, 16, 8, 16));
        }];
        [_thumb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self.card).offset(12);
            make.size.mas_equalTo(CGSizeMake(96, 72));
        }];
        [_titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.card).offset(12);
            make.left.equalTo(self.thumb.mas_right).offset(10);
            make.right.equalTo(self.card).offset(-12);
        }];
        [_introL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleL.mas_bottom).offset(6);
            make.left.right.equalTo(self.titleL);
        }];
        [_timeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.introL.mas_bottom).offset(6);
            make.left.right.equalTo(self.titleL);
            make.bottom.lessThanOrEqualTo(self.card).offset(-12);
        }];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    [self.thumb sd_cancelCurrentImageLoad];
    self.thumb.image = nil;
}

- (void)configureWithDict:(NSDictionary *)item {
    self.titleL.text = item[@"title"] ?: @"";
    self.introL.text = item[@"intro"] ?: @"";
    self.timeL.text  = item[@"time"] ?: @"";
    NSString *img = item[@"image"];
    if (img.length) {
        [self.thumb sd_setImageWithURL:[NSURL URLWithString:img]];
    }
}
@end
